﻿using System;
using System.Text;

namespace Tumblr.API.PostType
{
    public class Photo : Common
    {
        // This class will convert a file into a URL-encoded string
        // and store the string in sData. I thought of doing it in the  
        // btnPost_Click event, however I felt it would be a better 
        // design decision to have it as part of the Photo object.

        private string _sData;
        private string _sCaption;

        public string Source { get; set; }

        public string Data
        {
            get 
            {
                return _sData; 
            }
            // Check whether the file exists and stream it-->url-encode it
            set 
            {
                if (value.Length > 0)
                {
                    if (System.IO.File.Exists(value))
                    {
                        try
                        {
                            byte[] whole = System.IO.File.ReadAllBytes(value);
                            _sData = System.Web.HttpUtility.UrlEncode(whole);
                        }
                        catch (Exception e)
                        {
                            // Let the user know what went wrong.
                            throw e;
                        }
                    }
                    else
                    {
                        throw new Exception("There was something wrong with the File you selected. Please verify that the file exists.");

                    }
                }
            }
        }

        public string Caption
        {
            get
            {
                return _sCaption;
            }
            set
            {
                string s = value.Replace("\r", "");
                s = s.Replace("\n", "");
                _sCaption = System.Web.HttpUtility.UrlEncode(s, Encoding.UTF8);
            }
        }

        public string ClickThroughUrl { get; set; }
    }
}